package com.ssm.helong.shop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.helong.shop.Dao.AddressMapper;
import com.ssm.helong.shop.entity.Address;
import com.ssm.helong.shop.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;


    /***
     * 根据用户id查询地址
     * @param userId
     * @param pageNo
     * @param pageSize
     * @return: com.github.pagehelper.PageInfo<com.ssm.helong.shop.entity.Address>
     * @Author: helong.sun
     * @Date: 2021/5/8
     */
    @Override
    public PageInfo <Address> selectAddress(Integer userId, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize, true);
        List <Address> addressList = addressMapper.selectAddress(userId);
        PageInfo <Address> pageInfo = new PageInfo <>(addressList);
        return pageInfo;
    }

    /***
     * 根据地址编号修改地址信息
     * @param address
     * @return: java.lang.Boolean
     * @Author: helong.sun
     * @Date: 2021/5/8
     */
    @Override
    public Boolean updateAddress(Address address) {
        if (addressMapper.updateAddress(address) > 0) {
            return true;
        }
        return false;
    }

    /***
     * 添加新的地址
     * @param address
     * @return: java.lang.Boolean
     * @Author: helong.sun
     * @Date: 2021/5/8
     */
    @Override
    public Boolean addAddress(Address address) {
        if (addressMapper.addAddress(address) > 0) {
            return true;
        }
        return false;
    }

    /***
     * 根据地址编号删除地址信息
     * @param id
     * @return: java.lang.Boolean
     * @Author: helong.sun
     * @Date: 2021/5/8
     */
    @Override
    public Boolean deleteAddress(Integer id) {
        if (addressMapper.deleteAddress(id) > 0) {
            return true;
        }
        return false;
    }

    /***
     * 根据地址编号查询地址
     * @param id
     * @return: com.ssm.helong.shop.entity.Address
     * @Author: helong.sun
     * @Date: 2021/5/8
     */
    @Override
    public Address selectOneAddress(Integer id) {
        Address address = addressMapper.selectOneAddress(id);
        if (Objects.nonNull(address)) {
            return address;
        }
        return null;
    }


}
